<?php
    // product menu
    class MenuModel
    {
        public function __construct(PDO $pdo) {
            $this->pdo = $pdo;
        }
        public function insertMenu($arrayPost)
        {
            // get max order menu
            $sql = "SELECT MAX(ORDER_MENU) FROM set_menu;";
            $sth = $this->pdo->prepare($sql);
            $sth->execute();
            $result = $sth->fetch();
            $maxOrder = $result[0] + 1;
            $sql1 = "INSERT INTO set_menu (
                NAME_MENU,
                SEO_MENU,
                URL_MENU,
                ORDER_MENU
                ) 
                VALUES (
                :NAME_MENU,
                :SEO_MENU,
                :URL_MENU,
                :ORDER_MENU
                )";
            $stmp = $this->pdo->prepare($sql1);
            $result = $stmp->execute(array(
                                ':NAME_MENU'     =>$arrayPost['NAME_MENU'],
                                ':SEO_MENU'      =>$arrayPost['SEO_MENU'],
                                ':URL_MENU'      =>$arrayPost['URL_MENU'],
                                ':ORDER_MENU'    =>$maxOrder,                       
                        ));
                            
            return true;            
        }
        public function editMenu($arrayPost)
        {
            // get max order menu
            $sql = "UPDATE set_menu 
                    SET NAME_MENU=?, SEO_MENU=?,URL_MENU=?
            		WHERE ID_MENU=?";
            $stmp = $this->pdo->prepare($sql);
            $stmp->execute(array($arrayPost['NAME_MENU'],$arrayPost['SEO_MENU'],$arrayPost['URL_MENU'],$arrayPost['id']));          
            return true;            
        }        
        public function getAllMenu()
        {
            $sql = "SELECT * FROM set_menu order by ORDER_MENU";
            $sth = $this->pdo->prepare($sql);
            $sth->execute();
            $result = $sth->fetchAll();
            return $result;            
        }
    	public function buildMenu($menu, $parentid = 0) 
    	{ 
    	  $result = null;
    	  foreach ($menu as $item) 
    	    if ($item['IDPARENT_MENU'] == $parentid) { 
    	      $result .= "<li class='dd-item nested-list-item' data-order='".$item['ORDER_MENU']."' data-id='".$item['ID_MENU']."'>
    	      <div class='dd-handle nested-list-handle'>
    	        <span class='glyphicon glyphicon-move'></span>
    	      </div>
    	      <div class='nested-list-content'>".$item['NAME_MENU'].
    	        "<div class='pull-right'>
    	          <a href='".DOMAIN.'/'."application.php?mod=mod_admin&control=Menus&task=edit&id=".$item['ID_MENU']."'>Edit</a> |
    	          <a href='#' class='delete_toggle' rel='".$item['ID_MENU']."'>Delete</a>
    	        </div>
    	      </div>".$this->buildMenu($menu, $item['ID_MENU']) . "</li>"; 
    	    } 
    	  return $result ?  "\n<ol class=\"dd-list\">\n$result</ol>\n" : null; 
    	} 
    
    	// Getter for the HTML menu builder
    	public function getHTML($items)
    	{
    		return $this->buildMenu($items);
    	}
        function reorderMenu($arrayPost)
        {
            //print_r($arrayPost);exit();
            $source       = $arrayPost['source'];
    	    $destination  = isset($arrayPost['destination'])?$arrayPost['destination']:0;
            
            $sql = "UPDATE set_menu 
                    SET IDPARENT_MENU=?
            		WHERE ID_MENU=?";
            $sth = $this->pdo->prepare($sql);
            $sth->execute(array($destination,$source));
    
    	    $ordering       = json_decode($arrayPost['order']);
    	    $rootOrdering   = json_decode($arrayPost['rootOrder']);
    
    	    if($ordering){
    	      foreach($ordering as $order=>$item_id){
                    $sql1 = "UPDATE set_menu 
                            SET ORDER_MENU=?
                    		WHERE ID_MENU=?";
                    $stmp = $this->pdo->prepare($sql1);
                    $stmp->execute(array($order,$item_id));
    	      }
    	    } else {
    	      foreach($rootOrdering as $order=>$item_id){
                    $sql2 = "UPDATE set_menu 
                            SET ORDER_MENU=?
                    		WHERE ID_MENU=?";
                    $stmp1 = $this->pdo->prepare($sql2);
                    $stmp1->execute(array($order,$item_id));
    	      }
    	    }
    
    	    return 'ok ';
        }
        public function getMenuByID($id)
        {
            $sql = "SELECT * FROM set_menu WHERE ID_MENU= $id limit 1";
            $sth = $this->pdo->prepare($sql);
            $sth->execute();
            $result = $sth->fetch();
            return $result;             
        } 
        public function updateChildItem($id)
        {
            $sql = "SELECT * FROM set_menu WHERE IDPARENT_MENU= $id";
            $sth = $this->pdo->prepare($sql);
            $sth->execute();
            $result = $sth->fetchAll();
            if(count($result) > 0)
            {
                foreach($result as $row){           
                    $sql2 = "UPDATE set_menu 
                            SET IDPARENT_MENU=0
                    		WHERE ID_MENU=?";
                    $stmp1 = $this->pdo->prepare($sql2);
                    $stmp1->execute(array($row['ID_MENU'])); 
                }                
            }      
        }
        public function deleteItemMenu($id)
        {
            $sql = "DELETE FROM `set_menu` WHERE `ID_MENU` = $id";
            $sth = $this->pdo->prepare($sql);
            $sth->execute();            
        }     
    }
?>